package com.gg.filter;

import com.gg.entity.Category;
import com.gg.entity.Fruit;
import com.gg.helper.DBHelper;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebFilter(filterName = "LoadInitFilter", urlPatterns ="/*")
public class LoadInitFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
//        chain.doFilter(request, response);
        //先强转 数据类型
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse  res= (HttpServletResponse) response;
        /*判定Session中是否已经有10个热卖水果，  约定，10个热卖水果的名字叫hotfruits*/
        HttpSession session  =  req.getSession();
        List<Fruit> hotfruits = (List<Fruit>) session.getAttribute("hotfruits");

        /**********************新加的*******************************/
        /*获取Session中的类别们，categories，有则放行，无则拦截 */
        List<Category>  categories = (List<Category>) session.getAttribute("categories");
        /**********************新加的结束*******************************/
        if(hotfruits==null || categories==null){
            /*用户是第一次访问我这个服务器网站，需要让用户加载10个热卖水果*/
            Connection con = DBHelper.getConnection();
            String sql = "select * from fruit where f_hot = 1 limit 3 , 10";
            try {
                PreparedStatement ps = con.prepareStatement(sql);
                /*页面上需要fid，fname,fnorm*/
                ResultSet rs = ps.executeQuery();
                /*创建一个集合装10个水果*/
                List<Fruit>  list =  new ArrayList<>();
                /*循环解析结果集*/
                while (rs.next()){
                    Integer f_id = rs.getInt("f_id");
                    String f_name = rs.getString("f_name");
                    String f_norm = rs.getString("f_norm");
                    BigDecimal f_price = rs.getBigDecimal("f_price");
                    Fruit fruit =  new Fruit();
                    fruit.setF_id(f_id);
                    fruit.setF_name(f_name);
                    fruit.setF_norm(f_norm);
                    fruit.setF_price(f_price);
                    list.add(fruit);
                }
                /*把10个热卖水果放到Session里，好让在index.jsp显示。*/
                session.setAttribute("hotfruits", list);

                /**************************新加的******************************/
                categories =  new ArrayList<>();
                sql = "select *  from  category ";
                ps = con.prepareStatement(sql);
                rs = ps.executeQuery();
                while (rs.next()){
                    String c_id = rs.getString("c_id");
                    String c_name = rs.getString("c_name");
                    Category category = new Category(c_id, c_name);
                    categories.add(category);
                }
                /*把类别，放到Session里*/
                session.setAttribute("categories", categories);
                /*******************************新加的结束**********************************/
                /*跳转回主页*/
                res.sendRedirect("/fruit/index.jsp");

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }else{
            //放行！
            chain.doFilter(request, response);
        }
    }
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }


}
